import { FixedService } from './fixed.service';
import { Fixed, FixedResponse } from './fixed.interface';
import { CreateFixedDTO, EditFixedDTO } from './fixed.dto';
import {
    Body,
    Controller,
    Delete,
    Get,
    Param,
    Post
} from '@nestjs/common'

@Controller('fixed')
export class FixedController {
    constructor(private readonly fixedService: FixedService) {}

    @Get('all')
    async findAll() : Promise<FixedResponse<Fixed[]>> {
        return {
            code: 200,
            data: await this.fixedService.findAll(),
            msg: 'success'
        }
    }

    @Get(':_id')
    async find(@Param('_id') _id: string) : Promise<FixedResponse<Fixed>> {
        return {
            code: 200,
            data: await this.fixedService.find(_id),
            msg: 'success'
        }
    }

    @Post('getby')
    async findWhere(@Body() filter?: object) : Promise<FixedResponse<Fixed[]>> {
        return {
            code: 200,
            data: await this.fixedService.findWhere(filter),
            msg: 'success'
        }
    }

    @Post('add')
    async addone(@Body() body: CreateFixedDTO) : Promise<FixedResponse> {
        await this.fixedService.addone(body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Post('edit')
    async editone(@Body() body: EditFixedDTO) : Promise<FixedResponse> {
        await this.fixedService.editone(body._id, body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Delete(':_id')
    async deleteone(@Param('_id') _id: string) : Promise<FixedResponse> {
        return {
            code: 200,
            data: await this.fixedService.delete(_id),
            msg: 'success'
        }
    }
}
